package com.ssy.lingxi.order.model.constant;

import com.ssy.lingxi.common.language.LanguageHolder;
import com.ssy.lingxi.message.api.constant.MessageTemplateCode;
import com.ssy.lingxi.order.model.vo.basic.response.DropdownItem;
import com.ssy.lingxi.report.api.enums.PurchaseOrderOperateTypeEnum;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采购方订单内部状态枚举
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-15
 */
public enum BuyerInnerStatusEnum {
    /**
     *  采购订单：（新建订单）待提交审核 - 1
     */
    BUYER_TO_SUBMIT_VALIDATE(1, "待提交审核", PurchaseOrderOperateTypeEnum.TO_BE_VALIFY_COMMIT.getCode(), MessageTemplateCode.procurement_order_wait_audit),

    /**
     * 采购订单：待一级审核 - 2
     */
    BUYER_VALIDATE_GRADE_ONE(2, "待审核(一级)", PurchaseOrderOperateTypeEnum.TO_BE_VALIFY_STEP1.getCode(), MessageTemplateCode.procurement_order_audit_step_1),

    /**
     * 采购订单：一级审核不通过 - 3
     */
    BUYER_GRADE_ONE_NOT_PASSED(3, "审核不通过(一级)", 0, MessageTemplateCode.procurement_order_audit_result),

    /**
     * 采购订单：待二级审核 - 4
     */
    BUYER_VALIDATE_GRADE_TWO(4, "待审核(二级)", PurchaseOrderOperateTypeEnum.TO_BE_VALIFY_STEP2.getCode(), MessageTemplateCode.procurement_order_audit_step_2),

    /**
     * 采购订单：二级审核不通过 - 5
     */
    BUYER_GRADE_TWO_NOT_PASSED(5, "审核不通过(二级)", 0, MessageTemplateCode.procurement_order_audit_result),

    /**
     * 采购订单：待提交 - 6
     */
    BUYER_TO_SUBMIT(6, "待提交", PurchaseOrderOperateTypeEnum.TO_BE_COMMIT.getCode(), MessageTemplateCode.procurement_order_submit),

    /**
     * 采购订单：已提交 - 7
     */
    BUYER__SUBMITTED(7, "已提交", 0, ""),

    /**
     * 采购订单：待确认电子合同 - 8
     */
    BUYER_TO_CONFIRM_CONTRACT(8, "待确认电子合同", PurchaseOrderOperateTypeEnum.TO_BE_CONFIRM_CONTRACT.getCode(), MessageTemplateCode.procurement_order_contract),

    /**
     * 采购订单：已确认电子合同 - 9
     */
    BUYER__CONFIRMED(9, "已确认电子合同", 0, ""),

    /**
     * 采购订单：未支付 - 10
     */
    BUYER_TO_PAY(10, "待支付", PurchaseOrderOperateTypeEnum.TO_BE_PAY.getCode(), MessageTemplateCode.procurement_order_payment),

    /**
     * 采购订单：支付中 - 19
     */
    BUYER_PAYING(19, "支付中", 0, ""),

    /**
     * 采购订单：支付成功 - 11
     */
    BUYER_PAY_SUCCESS(11, "支付成功", 0, ""),

    /**
     * 采购订单：支付失败 - 12
     */
    BUYER_PAY_FAIL(12, "支付失败", 0, ""),

    /**
     * 采购订单：待新增采购入库单 - 13
     */
    BUYER_TO_DEPOSIT(13, "待新增采购入库单", PurchaseOrderOperateTypeEnum.TO_BE_ADD_STORAGE.getCode(), MessageTemplateCode.procurement_order_storage),

    /**
     * 采购订单：待审核采购入库单 - 14
     */
    BUYER_TO_VALIDATE_DEPOT(14, "待审核采购入库单", PurchaseOrderOperateTypeEnum.TO_BE_ADD_STORAGE.getCode(), ""),

    /**
     * 采购订单：已审核采购入库单 - 15
     */
    BUYER_DEPOT_PASSED(15, "已审核采购入库单", 0, ""),

    /**
     * 采购订单：待确认收货 - 16
     */
    BUYER_TO_RECEIVE(16, "待确认收货", PurchaseOrderOperateTypeEnum.TO_BE_CONFIRM_RECEIVE_GOODS.getCode(), MessageTemplateCode.procurement_order_receive),

    /**
     * 采购订单：已确认收货 - 17
     */
    BUYER_CONFIRM_RECEIPT(17, "已确认收货", 0, ""),

    /**
     * 采购订单：待归档 - 18
     */
    BUYER_TO_ARCHIVE(18, "待归档", PurchaseOrderOperateTypeEnum.TO_BE_ARCHIVE.getCode(), MessageTemplateCode.procurement_order_archive),

    /*  ***************************************
     *  ******** (供采双方共同)内部状态 *********
     *  ***************************************
     */

    /**
     * 订单：已完成 - 1000
     */
    ACCOMPLISHED(1000, "已完成", 0, ""),

    /**
     * 订单：已取消 - 1001
     */
    CANCELLED(1001, "已取消", 0, ""),

    /**
     * 订单：已中止 - 1002
     */
    TERMINATED(1002, "已中止", 0, ""),

    /**
     * 订单：已结算 - 1003
     */
    HAVE_SETTLED(1003, "已结算", 0, "");


    BuyerInnerStatusEnum(Integer code, String name, Integer reportTypeCode, String messageCode) {
        this.code = code;
        this.reportTypeCode = reportTypeCode;
        this.messageCode = messageCode;
    }

    /**
     * 状态值
     */
    private Integer code;

    /**
     * 向报表服务发送数据请求的code，对应 PurchaseOrderOperateTypeEnum
     */
    private Integer reportTypeCode;

    /**
     * 向消息服务发送数据请求的code，对应 MessageTemplateCode
     */
    private String messageCode;

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return LanguageHolder.getName(this.getClass(), this.code);
    }

    public Integer getReportTypeCode() {
        return reportTypeCode;
    }

    public String getMessageCode() {
        return messageCode;
    }

    /**
     * 根据枚举值获得状态名称
     * @param code 枚举值
     * @return 状态名称
     */
    public static String getNameByCode(Integer code) {
        return LanguageHolder.getName(BuyerInnerStatusEnum.class, code);
    }

    /**
     * 内部状态列表
     * @return 前端下拉框状态列表
     */
    public static List<DropdownItem> toDropdownList() {
        String key = BuyerInnerStatusEnum.class.getSimpleName().concat(".");
        return Arrays.stream(BuyerInnerStatusEnum.values()).map(e -> new DropdownItem(e.getCode(), LanguageHolder.getName(key.concat(String.valueOf(e.getCode()))))).collect(Collectors.toList());
    }

    /**
     * 根据枚举值转换为枚举
     * @param code 枚举值
     * @return 枚举
     */
    public static BuyerInnerStatusEnum parse(Integer code) {
        return code == null ? null : Arrays.stream(BuyerInnerStatusEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
    }

    /**
     * 根据内部状态枚举值，获得报表服务的操作code
     * @param code 内部状态枚举
     * @return 报表服务定义的 PurchaseOrderOperateTypeEnum
     */
    public static Integer getReportCode(Integer code) {
        BuyerInnerStatusEnum statusEnum = Arrays.stream(BuyerInnerStatusEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
        return statusEnum == null ? 0 : statusEnum.getReportTypeCode();
    }

    /**
     * 根据内部状态枚举值，获得消息服务的操作code
     * @param code 内部状态枚举
     * @return 消息服务定义的 MessageTemplateCode
     */
    public static String getMessageCode(Integer code) {
        BuyerInnerStatusEnum statusEnum = Arrays.stream(BuyerInnerStatusEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
        return statusEnum == null ? "" : statusEnum.getMessageCode();
    }
}
